{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# Comparableモジュール\n",
    "\n",
    "* <=>メソッド(比較演算子)を元に<font color=\"red\">オブジェクト同士の比較ができるようになる</font>\n",
    "* 利用できるようになるインスタンスメソッド\n",
    "    * <=>\n",
    "    * <=\n",
    "    * ==\n",
    "    * >\n",
    "    * `>=`\n",
    "    * between?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       ":<=>"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "class Sample\n",
    "  include Comparable\n",
    "  \n",
    "  def initialize(value)\n",
    "    @value = value\n",
    "  end\n",
    "  \n",
    "  def value\n",
    "    @value\n",
    "  end\n",
    "  \n",
    "  def <=>(other)\n",
    "    self.value <=> other.value\n",
    "  end\n",
    "end"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[Sample, Comparable, Object, PP::ObjectMixin, JSON::Ext::Generator::GeneratorMethods::Object, Kernel, BasicObject]\n",
      "false\n",
      "false\n",
      "true\n",
      "true\n",
      "true\n"
     ]
    }
   ],
   "source": [
    "puts Sample.ancestors\n",
    "\n",
    "a = Sample.new(10)\n",
    "b = Sample.new(5)\n",
    "puts a <= b\n",
    "puts a == b\n",
    "puts a > b\n",
    "puts a >= b\n",
    "\n",
    "c = Sample.new(2)\n",
    "puts b.between?(c, a)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Ruby 2.3.1",
   "language": "ruby",
   "name": "ruby"
  },
  "language_info": {
   "file_extension": ".rb",
   "mimetype": "application/x-ruby",
   "name": "ruby",
   "version": "2.3.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
